
package rendi456.structure.grid;

public class GridFocus<E extends GridCell>
{
	protected final Grid<E> grid;
	protected E cell = null;

	public GridFocus(Grid<E> grid)
	{
		this.grid = grid;
		this.cell = this.grid.getNode(0);
	}

	public boolean setAddress(int index)
	{
		boolean ok = this.grid.checkIndex(index);
		if (ok)
		{
			this.cell = this.grid.getNode(index);
		}
		return ok;
	}

	public boolean setAddress(int row, int col)
	{
		boolean ok = this.grid.checkAddress(row, col);
		if (ok)
		{
			this.cell = this.grid.getNode(row, col);
		}
		return ok;
	}

	public boolean setAddress(GridAddress a)
	{
		return this.setAddress(a.getRow(), a.getColumn());
	}

	public void gotoFocus(GridFocus<E> focus)
	{
		this.cell = focus.cell;
	}

	public boolean move(int dRow, int dCol)
	{
		int newRow = this.cell.getRow() + dRow;
		int newCol = this.cell.getColumn() + dCol;
		return this.setAddress(newRow, newCol);
	}

	public int getIndex()
	{
		return this.cell.index;
	}

	public GridAddress getAddress()
	{
		return this.cell.getAddress();
	}

	public int getColumn()
	{
		return this.cell.getColumn();
	}

	public int getRow()
	{
		return this.cell.getRow();
	}

	public boolean hasNext()
	{
		return this.cell.index + 1 < this.grid.size;
	}

	public void rewind()
	{
		this.setAddress(0);
	}

	public void next()
	{
		this.setAddress(this.cell.index + 1);
	}

}
